package com.x.ant.dao;

import com.x.ant.model.Money;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class MoneyDao extends BaseDao {

    private final static String selectMoneyListSql = "SELECT T1.* FROM ANT_MONEY T1, ANT_DEPT T2 WHERE T1.DEPT_ID = T2.DEPT_ID ";
    public List<Money> selectMoneyList(Money money) throws Exception{
        StringBuilder sb = new StringBuilder(selectMoneyListSql);
        List params = new ArrayList();
        if (this.isString(money.getDeptId())) {
            sb.append("AND T1.DEPT_ID = ? ");
            params.add(money.getDeptId());
        }
        if (this.isString(money.getTaskId())) {
            sb.append("AND T1.TASK_ID = ? ");
            params.add(money.getTaskId());
        }
        if (money.getMoneyInOut() != null) {
            sb.append("AND T1.MONEY_IN_OUT = ? ");
            params.add(money.getMoneyInOut());
        }
        if (money.getMoneyType() != null) {
            sb.append("AND T1.MONEY_TYPE = ? ");
            params.add(money.getMoneyType());
        }
        if (money.getMoneyState() != null) {
            sb.append("AND T1.MONEY_STATE = ? ");
            params.add(money.getMoneyState());
        }
        if (money.getMoneyCreateTime() != null) {
            sb.append("AND T1.MONEY_CREATE_TIME = ? ");
            params.add(money.getMoneyCreateTime());
        }
        sb.append("ORDER BY T1.MONEY_CREATE_TIME DESC");
        return this.queryForList(sb.toString(), Money.class, params, money);
    }

    private final static String selectMoneyNumSql = "SELECT COUNT(1) FROM ANT_MONEY T1, ANT_DEPT T2 WHERE T1.DEPT_ID = T2.DEPT_ID ";
    public int selectMoneyNum(Money money) throws Exception{
        StringBuilder sb = new StringBuilder(selectMoneyNumSql);
        List params = new ArrayList();
        if (this.isString(money.getDeptId())) {
            sb.append("AND T1.DEPT_ID = ? ");
            params.add(money.getDeptId());
        }
        if (this.isString(money.getTaskId())) {
            sb.append("AND T1.TASK_ID = ? ");
            params.add(money.getTaskId());
        }
        if (money.getMoneyInOut() != null) {
            sb.append("AND T1.MONEY_IN_OUT = ? ");
            params.add(money.getMoneyInOut());
        }
        if (money.getMoneyType() != null) {
            sb.append("AND T1.MONEY_TYPE = ? ");
            params.add(money.getMoneyType());
        }
        if (money.getMoneyState() != null) {
            sb.append("AND T1.MONEY_STATE = ? ");
            params.add(money.getMoneyState());
        }
        if (money.getMoneyCreateTime() != null) {
            sb.append("AND T1.MONEY_CREATE_TIME = ? ");
            params.add(money.getMoneyCreateTime());
        }
        return this.queryForInt(sb.toString(), params);
    }

}
